(function() {

    var Part = function(source) {
        this.initialize(source);
    };


    var p = Part.prototype = new createjs.Container(); // inherit from Container

    this.dragger;
    p.bmp;
    p.background;
    p.hitScalePixel = 100; //px
    this.source;
    
    this.previousX;
    this.previousY;
    
    p.width = 0;
    p.height = 0;
    
    this.hitWidth = p.width + p.hitScalePixel;
    this.hitHeight = p.height + p.hitScalePixel;
    
    p.Container_initialize = p.initialize;
    p.initialize = function(source) {
        this.Container_initialize();
        this.source = source;
        p.bmp = new createjs.Bitmap(queue.getResult(source));
        
//        var shadow = new createjs.Shadow("#000000", 10, 10, 15);
//        p.bmp.shadow = shadow;
        p.addChild(p.bmp);

        p.width = p.bmp.getBounds().width;
        p.height = p.bmp.getBounds().height;

        this.hitWidth = p.width + p.hitScalePixel;
        this.hitHeight = p.height + p.hitScalePixel;

        // this lets our drag continue to track the mouse even when it leaves the canvas:
        // play with commenting this out to see the difference.
        //stage.mouseMoveOutside = true; 

//        var circle = new createjs.Shape();
//        circle.graphics.beginFill("red").drawCircle(0, 0, 50);
//
//        var label = new createjs.Text("drag me", "bold 14px Arial", "#FFFFFF");
//        label.textAlign = "center";
//        label.y = -7;

        var ba = new createjs.Shape();
//	ba.graphics.beginStroke("#000000").rect(0,0,p.bmp.getBounds().width,p.bmp.getBounds().height);

        this.dragger = new createjs.Container();
        this.dragger.x = this.dragger.y = 100;
        this.dragger.addChild(p.bmp, ba);
        stage.addChild(this.dragger);

        this.dragger.on("pressmove", function(evt) {
            var bounds = p.bmp.getBounds();
            // currentTarget will be the container that the event listener was added to:
            evt.currentTarget.x = evt.stageX - (p.width / 2);
            evt.currentTarget.y = evt.stageY - (p.height / 2);
            // make sure to redraw the stage to show the change:
            stage.update();
        });
        
        this.dragger.on("mouseover", function(evt) {
            // make sure to redraw the stage to show the change:
            stage.update();
        });
        
        this.on("tick", this.handleTick);
        this.mouseChildren = false;
    };

    p.removeChild = function() {
        stage.removeChild(this.dragger);
    };

    p.addChild = function() {
        stage.addChild(this.dragger);
    };

    p.hitTest = function(mouseX, mouseY) {
        var bounds = p.bmp.getBounds();
        var wid = p.bmp.scaleX * bounds.width;
        var heg = p.bmp.scaleY * bounds.height;
        return this.dragger.x <= mouseX && mouseX <= this.dragger.x + wid
            && this.dragger.y <= mouseY && mouseY <= this.dragger.y + heg;
    };

    p.calculateHitPixel = function() {
        this.hitWidth = p.width + p.hitScalePixel;
        this.hitHeight = p.height + p.hitScalePixel;
    };

    p.appearLine = function(fromX, fromY, moveX, moveY) {
        var bounds = p.bmp.getBounds();
        //move the form above the screen
        this.dragger.x = fromX;
        this.dragger.y = fromY;
        createjs.Tween.get(this.dragger).to({alpha: 1, x: moveX, y: moveY}, 2000, createjs.Ease.linear);
    };

    p.hitScale = function() {
        p.sizeUnchangeHitSize(this.hitWidth, this.hitHeight);
    };

    p.unhitScale = function() {
        p.sizeUnchangeHitSize(p.width, p.height);
    };

    p.position = function(x, y) {
        this.dragger.x = x;
        this.dragger.y = y;
        p.previousX = x;
        p.previousY = y;
    };
    
    p.disappearScale = function() {
        p.alpha = 0;
        createjs.Tween.get(this.dragger).to({scaleX:0,scaleY:0,alpha:1}, 2000, createjs.Ease.backIn);
    };
    
    p.size = function(windowWidth, windowHeight) {
        var bounds = p.bmp.getBounds();
        var currentWidth = bounds.width;
        var currentHeight = bounds.height;

        // Derive a scale. Use the smaller of the two to "fit" the image. Change to Math.max to make it "fill" the window
        var scale = Math.min(windowWidth / currentWidth, windowHeight / currentHeight);
        p.bmp.scaleX = p.bmp.scaleY = scale;
        p.width = windowWidth * p.bmp.scaleX;
        p.height = windowHeight * p.bmp.scaleY;
        p.calculateHitPixel();
    };

    p.sizeUnchangeHitSize = function(windowWidth, windowHeight) {
        var bounds = p.bmp.getBounds();
        var currentWidth = bounds.width;
        var currentHeight = bounds.height;

        // Derive a scale. Use the smaller of the two to "fit" the image. Change to Math.max to make it "fill" the window
        var scale = Math.min(windowWidth / currentWidth, windowHeight / currentHeight);
        p.bmp.scaleX = p.bmp.scaleY = scale;
    };
    
    p.moveSpin = function(canvasWidth, canvasHeight) {
        var bounds = p.bmp.getBounds();
        createjs.Tween.get(p.bmp, {loop: true}).to({x: 450}, 30000).to({x: 50}, 30000);
    };

    p.tweenComplete = function(fn) {

    };

    p.handleClick = function(event) {
        var target = event.target;
        alert("You clicked on a button: " + target.label);
    };

    p.handleTick = function(event) {
//        myParts[index].unhitScale();
//        if (myParts[index].hitTest(stage.mouseX, stage.mouseY)) {
//            myParts[index].hitScale();
//        }
    };

    window.Part = Part;
}());